package com.cicd.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cicd.entity.User;
import com.cicd.service.UserService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/users")
@Slf4j
public class UserController {

  @Autowired private UserService userService;

  @PostMapping("/callback")
  public Map<String, Object> callback(@RequestBody Map<String, Object> data) {
    log.info("Received callback data: {}", data);
    Map<String, Object> result = new HashMap<>();
    result.put("code", 200);

    return result;
  }

  /** 获取所有用户 */
  @GetMapping
  public List<User> getAllUsers() {
    return userService.list();
  }

  // 分页查询用户
  @GetMapping("/page")
  public Page<User> getUsersByPage(@RequestParam int current, @RequestParam int size) {
    return userService.page(new Page<>(current, size));
  }

  // 根据ID查询用户
  @GetMapping("/{id}")
  public User getUserById(@PathVariable Long id) {
    return userService.getById(id);
  }

  // 添加用户
  @PostMapping
  public boolean addUser(@RequestBody User user) {
    return userService.save(user);
  }

  // 更新用户
  @PutMapping("/{id}")
  public boolean updateUser(@PathVariable Long id, @RequestBody User user) {
    user.setId(id);
    return userService.updateById(user);
  }

  // 删除用户
  @DeleteMapping("/{id}")
  public boolean deleteUser(@PathVariable Long id) {
    return userService.removeById(id);
  }
}
